package com.suep.data_analysis.service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.suep.data_analysis.dto.AgentDetailDto;
import com.suep.data_analysis.dto.AgentDto;
import com.suep.data_analysis.mapper.AgentDetailMapper;
import com.suep.data_analysis.mapper.AgentMapper;
import com.suep.data_analysis.model.Agent;
import com.suep.data_analysis.model.AgentAttribute;
import com.suep.data_analysis.model.AgentSkills;
import com.suep.data_analysis.repository.AgentAttributeRepository;
import com.suep.data_analysis.repository.AgentRepository;
import com.suep.data_analysis.repository.AgentSkillsRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class AgentService {
    private final AgentRepository agentRepository;
    private final AgentAttributeRepository attributeRepository;
    private final AgentSkillsRepository skillsRepository;
    private final AgentMapper agentMapper;
    private final AgentDetailMapper detailMapper;

    /**
     * 查询所有代理人列表（基础信息）
     */
    public List<AgentDto> getAllAgents() {
        List<Agent> agents = agentRepository.selectAllAgent();
        return agents.stream()
                .map(agentMapper::toAgentDto)
                .collect(Collectors.toList());
    }

    /**
     * 根据ID查询代理人详情（含属性和技能）
     */
    public AgentDetailDto getAgentById(String agentId) {
        Objects.requireNonNull(agentId, "代理人ID不能为null");

        // 查询基础信息
        Wrapper<Agent> agentQuery = Wrappers.<Agent>lambdaQuery()
                .eq(Agent::getAgentId, agentId);
        Agent agent = agentRepository.selectAgent(agentQuery).stream()
                .findFirst()
                .orElse(null);

        if (agent == null) {
            return null;
        }

        // 查询属性
        AgentAttribute attribute = attributeRepository.selectByAgentId(agentId);

        // 查询技能
        List<AgentSkills> skills = skillsRepository.selectAllByAgentId(agentId);

        // 组装详情DTO
        return detailMapper.toAgentDetailDto(agent, attribute, skills);
    }

    /**
     * 根据关键词搜索代理人（支持名称、职业、阵营等字段）
     */
    public List<AgentDto> searchAgents(String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return getAllAgents(); // 空关键词返回全部
        }

        // 构建多字段搜索条件
        Wrapper<Agent> query = Wrappers.<Agent>lambdaQuery()
                .like(Agent::getAgentName, keyword)
                .or()
                .like(Agent::getProfession, keyword)
                .or()
                .like(Agent::getFaction, keyword)
                .or()
                .like(Agent::getDescription, keyword);

        // 执行查询并转换为DTO
        List<Agent> agents = agentRepository.selectAgent(query);
        return agents.stream()
                .map(agentMapper::toAgentDto)
                .collect(Collectors.toList());
    }
}